package cn.chinaunicom.smart.community.generator.rest;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.chinaunicom.smart.community.generator.constant.GenConstant;
import cn.chinaunicom.smart.community.generator.entity.Column;
import cn.chinaunicom.smart.community.generator.entity.GeneratorConfig;
import cn.chinaunicom.smart.community.generator.helper.GeneratorHelper;
import cn.chinaunicom.smart.community.generator.po.vo.GeneratorConfigVO;
import cn.chinaunicom.smart.community.generator.service.IGeneratorService;
import cn.chinaunicom.smart.community.generator.util.FileUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author MrBird
 */
@Slf4j
@RestController
@RequestMapping("generator")
public class GeneratorController {

    private static final String SUFFIX = "_code.zip";

    @Autowired
    private IGeneratorService generatorService;

    @Autowired
    private GeneratorConfigVO genConfVO;
    @Autowired
    private GeneratorHelper generatorHelper;

//    @GetMapping("tables")
//    public Result tablesInfo(String tableName, QueryRequest request) {
//        Map<String, Object> dataTable = FebsUtil.getDataTable(generatorService.getTables(tableName, request, genConfVO.getDatabaseType(), genConfVO.getDatabaseName()));
//        return Result.success(dataTable);
//    }

    @RequestMapping("genCode")
    public void generate(@NotBlank(message = "{required}") String name, String remark, HttpServletResponse response) throws Exception {
        GeneratorConfig generatorConfig = genConfVO.toEntity();
        if (generatorConfig == null) {
        	log.error("代码生成器配置对象未注入");
            throw new Exception("代码生成配置为空");
        }

        String className = name;
        if (GeneratorConfig.TRIM_YES.equals(generatorConfig.getIsTrim())) {
            className = RegExUtils.replaceFirst(name, generatorConfig.getTrimValue(), StringUtils.EMPTY);
        }

        generatorConfig.setTableName(name);
//        generatorConfig.setClassName(FebsUtil.underscoreToCamel(className));
        generatorConfig.setTableComment(remark);
        // 生成代码到临时目录
        List<Column> columns = generatorService.getColumns(genConfVO.getDatabaseType(), genConfVO.getDatabaseName(), name);
        generatorHelper.generateEntityFile(columns, generatorConfig);
        generatorHelper.generateMapperFile(columns, generatorConfig);
        generatorHelper.generateMapperXmlFile(columns, generatorConfig);
        generatorHelper.generateServiceFile(columns, generatorConfig);
        generatorHelper.generateServiceImplFile(columns, generatorConfig);
        generatorHelper.generateControllerFile(columns, generatorConfig);
        // 打包
        String zipFile = System.currentTimeMillis() + SUFFIX;
        FileUtil.compress(GenConstant.TEMP_PATH + "src", zipFile);
        // 下载
        FileUtil.download(zipFile, name + SUFFIX, true, response);
        // 删除临时目录
        FileUtil.delete(GenConstant.TEMP_PATH);
    }
}
